package netbayWatchDog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;

public class SFTPManager {

	public static String SFTPuser;
	public static String SFTPhost;
	public static Integer SFTPport;
	public static String SFTPprivateKeyPath;
	public static String SFTPpassPhrase;
	public static boolean SFTPknowHostStrict;

	public static String SFTPrecvPath = "/Outbound/Encrypted";

	private static ChannelSftp mySftp;

	public boolean lookForNewfile() {

		WatchDogUtility.initialize();
		try {
			ChannelSftp cs = new SFTPManager().getChannelSFTP();

			Vector v = null;

			v = cs.ls(SFTPrecvPath);
			for (int i = 0; i < v.size(); ++i) {

				Object obj = v.get(i);
				if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
					String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj)
							.getFilename();
					if (!fileName.equalsIgnoreCase(".")
							&& !fileName.equalsIgnoreCase(".."))
						return true;
				}
			}

		} catch (JSchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	private ChannelSftp getChannelSFTP() throws JSchException, SftpException {

		if (mySftp != null && !mySftp.isConnected())mySftp.connect();
		
		if (mySftp == null || !mySftp.isConnected()) {

			JSch jSch = new JSch();
			byte[] prvkey = null;

//			System.out.println(SFTPpassPhrase);

			byte[] passPhrase = SFTPpassPhrase.getBytes();

			try {

				prvkey = readMyPrivateKeyFromFile();
				// Private key must be byte array

				// passPhrase = readMyPassPhraseFromFile();
				// Private key must be byte array
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

//			System.out.println(" read prv key ");

			jSch.addIdentity(SFTPuser, // String userName
					prvkey, // byte[] privateKey
					null, // byte[] publicKey
					passPhrase // byte[] passPhrase
			);

//			System.out.println("finish read prv key ");

			Session session = jSch.getSession(SFTPuser, SFTPhost, SFTPport);

			UserInfo ui = new MyUserInfo(SFTPManager.SFTPpassPhrase); // MyUserInfo
																		// implements
																		// UserInfo
			session.setUserInfo(ui);
			java.util.Properties config = new java.util.Properties();

			if (!SFTPknowHostStrict) {
				config.put("StrictHostKeyChecking", "no");
				session.setConfig(config);
			}

			session.connect();
			Channel channel = session.openChannel("sftp");
			ChannelSftp sftp = (ChannelSftp) channel;
			sftp.connect();

//			System.out.println("successfully connect with sftp");

			mySftp = sftp;

		}

		return mySftp;
	}

	private byte[] readMyPrivateKeyFromFile() throws IOException {

		File prvkey = new File(SFTPprivateKeyPath);

//		System.out.println(prvkey.getAbsolutePath());
//		System.out.println(prvkey.getName());

		Long fileLen = prvkey.length();
		FileInputStream fileStream = new FileInputStream(prvkey);

		byte fileContent[] = new byte[fileLen.intValue()];
		fileStream.read(fileContent);

//		System.out.println();

		return fileContent;

	}
}
